﻿using System;
using System.Collections.Generic;
using System.Text;

using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.DataSourcesRaster;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.esriSystem;

namespace SZ
{
  public   class ElementManageClass
    {
        //颜色
        public IRgbColor GetColor(int red, int green, int blue)
        {
            RgbColor colcor = new RgbColor();
            colcor.Red = red;
            colcor.Green = green;
            colcor.Blue = blue;
            return colcor;
        }

        //创建元素
        public IElement CreateElement(IGeometry pGeometry, IRgbColor rgbColor, IRgbColor outlineRgbColor)
        {
            IElement pElement = null;
            //点
            if (pGeometry.GeometryType == esriGeometryType.esriGeometryPoint)
            {
                // marker symbol 的中文意思为点符号
                ISimpleMarkerSymbol pSimpleMarkerSymbol = new SimpleMarkerSymbolClass();
                pSimpleMarkerSymbol.Color = rgbColor;
                pSimpleMarkerSymbol.Outline = true;
                pSimpleMarkerSymbol.OutlineColor = outlineRgbColor;
                pSimpleMarkerSymbol.Size = 8;
                pSimpleMarkerSymbol.Style = esriSimpleMarkerStyle.esriSMSCircle;

                IMarkerElement markerElement = new MarkerElementClass();
                markerElement.Symbol = pSimpleMarkerSymbol;
                pElement = markerElement as IElement;
            }
            //多义线
            else if (pGeometry.GeometryType == esriGeometryType.esriGeometryPolyline)
            {
                ISimpleLineSymbol simpleLineSymbol = new SimpleLineSymbolClass();
                simpleLineSymbol.Color = rgbColor;
                simpleLineSymbol.Style = esriSimpleLineStyle.esriSLSSolid;
                simpleLineSymbol.Width = 2;

                ILineElement pLineElement = new LineElementClass();
                pLineElement.Symbol = simpleLineSymbol;
                pElement = pLineElement as IElement;
            }
            //多边形
            else if (pGeometry.GeometryType == esriGeometryType.esriGeometryPolygon)
            {
                //填充
                ISimpleFillSymbol simpleFillSymbol = new SimpleFillSymbolClass();
                simpleFillSymbol.Color = rgbColor;
                simpleFillSymbol.Color.Transparency = 100;
                simpleFillSymbol.Style = esriSimpleFillStyle.esriSFSNull;

                //边界线
                ISimpleLineSymbol simpleLineSymbol = new SimpleLineSymbolClass();
                simpleLineSymbol.Color = outlineRgbColor;
                simpleLineSymbol.Style = esriSimpleLineStyle.esriSLSSolid;
                simpleLineSymbol.Width = 2;

                //将边界线赋给填充的外边界线
                simpleFillSymbol.Outline = simpleLineSymbol;

                IFillShapeElement pPolygonElement = new PolygonElementClass();
                pPolygonElement.Symbol = simpleFillSymbol;
                pElement = pPolygonElement as IElement;
            }

            pElement.Geometry = pGeometry;
            return pElement;
        }

        //添加图形元素
        public bool AddElement(IMap pMap, IElement pElement, string layerName)
        {
            IGraphicsLayer pGraphicsLayer = null;
            pGraphicsLayer = CreateGraphicsLayer(pMap, layerName);
            if (pGraphicsLayer != null)
            {
                IGraphicsContainer pGraphicsContainer = pGraphicsLayer as IGraphicsContainer;
                pGraphicsContainer.DeleteAllElements();
                pGraphicsContainer.AddElement(pElement, 0);
                IActiveView pActiveView = pMap as IActiveView;
                pActiveView.Refresh();
                return true;
            }
            else
                return false;
        }
        
        //获得图形图层
        public IGraphicsLayer GetGraphicsLayer(IMap pMap, string graphicsLayerName)
        {
            ICompositeGraphicsLayer pCompositeGraphicsLayer = pMap.BasicGraphicsLayer as ICompositeGraphicsLayer;
            IGraphicsLayer pGraphicsLayer = null;

            try
            {
                pGraphicsLayer = pCompositeGraphicsLayer.FindLayer(graphicsLayerName);
            }
            catch
            {
                return null;
            }
            return pGraphicsLayer;
        }

        //创建图形图层

        public IGraphicsLayer CreateGraphicsLayer(IMap pMap, string graphicsLayerName)
        {
            IGraphicsLayer pGraphicsLayer = GetGraphicsLayer(pMap, graphicsLayerName);
            if (pGraphicsLayer == null)
            {
                ICompositeGraphicsLayer pCompositeGraphicsLayer = pMap.BasicGraphicsLayer as ICompositeGraphicsLayer;
                pGraphicsLayer = pCompositeGraphicsLayer.AddLayer(graphicsLayerName, null);
            }
            return pGraphicsLayer;
        }
  
        //清空某个图层中的所有元素
        public void ClearAllElementInLayer(IMap pMap, string layerName)
        {
            ILayer pLayer = null;
            int layerCount = pMap.LayerCount;
            for (int i = 0; i < layerCount; i++)
            {
                pLayer = pMap.get_Layer(i);
                if (pLayer.Name == layerName)
                {
                    return;
                }
            }
            IGraphicsLayer pGraphicsLayer = pLayer as IGraphicsLayer;
            IGraphicsContainer pGraphicsContainer = pGraphicsLayer as IGraphicsContainer;
            pGraphicsContainer.DeleteAllElements();

            IActiveView pActiveView = pMap as IActiveView;
            pActiveView.Refresh();
        }
    }

}
